import { Effect, Subscription } from 'dva';
import { Reducer } from 'redux';
import { selectAll } from '@/services/boss';

export interface Boss {
  address: string;
  contect: string;
  education: string;
  endYear: number;
  id: number;
  peopleNum: number;
  positionCommand: string;
  positionDuty: string;
  postion: string;
  startYear: number;
}

export interface BossStateType {
  data: Array<Boss>;
}

interface BossModelType {
  namespace: 'bosss';
  state: BossStateType;
  reducers: {
    saveBosss: Reducer<BossStateType>;
  };
  effects: {
    fetchBosss: Effect;
  };
  subscriptions: {
    setup: Subscription;
  };
}

const BossModel: BossModelType = {
  namespace: 'bosss',
  state: { data: [] },
  reducers: {
    saveBosss(state = { data: [] }, { payload }) {
      return {
        data: [...payload.data],
      };
    },
  },
  effects: {
    *fetchBosss({ payload }, { put, call }) {
      debugger;
      let data: BossStateType = yield call(selectAll, { deptIds: payload.deptIds });
      yield put({ type: 'saveBosss', payload: data });
    },
  },
  subscriptions: {
    setup({ history, dispatch }) {
      // history.listen((location)=>{
      //     if (location.pathname == '/boss/list') {
      //         dispatch({
      //             type: 'fetchBosss'
      //         })
      //     }
      // })
    },
  },
};

export default BossModel;
